import 'dart:async';

import 'package:flutter/material.dart';
import 'package:wheel_of_fortune/http/wheel_server.dart';
import 'package:wheel_of_fortune/model/address_model.dart';
import 'package:wheel_of_fortune/utils/utils.dart';

class AddressProvider extends ChangeNotifier {
  List<RegionModel> regions = [];
  List<ProvinceModel> provinces = [];
  List<CityModel> citys = [];

  List<Widget> regionItems = [];
  List<Widget> provinceItems = [];
  List<Widget> cityItems = [];
  final context;

  AddressProvider(this.context);
  initData() {
    getProvinces(null);
  }

  getRegions() async {
    try {
      provinceItems = [];
      cityItems = [];
      regions = await WheelService().getRegionlist();
      regions.forEach((item) {
        final widget = Container(
          alignment: Alignment.center,
          height: 30,
          child: Text(
            '${item.regionName}',
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: 12),
          ),
        );
        regionItems.add(widget);
      });
    } catch (e) {
      print('e = $e');
    }
    notifyListeners();
  }

  getProvinces(regionCode) async {
    try {
      provinceItems = [];
      cityItems = [];
      // provinces = await WheelService().getProvincelist(regionCode);
      provinces = await AddressHelper.getProvinceList(this.context,
          regionCode: regionCode);
      provinces.forEach((item) {
        final widget = Container(
          alignment: Alignment.center,
          height: 30,
          child: Text(
            '${item.provinceName}',
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: 12),
          ),
        );
        provinceItems.add(widget);
      });
    } catch (e) {
      print('e = $e');
    }
    notifyListeners();
  }

  getCitys(provinceCode) async {
    try {
      cityItems = [];
      // citys = await WheelService().getCitylist(provinceCode);
      citys = await AddressHelper.getCityList(this.context,
          provinceCode: provinceCode);
      citys.forEach((item) {
        final widget = Container(
          alignment: Alignment.center,
          height: 30,
          child: Text(
            '${item.cityName}',
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: 12),
          ),
        );
        cityItems.add(widget);
      });
    } catch (e) {
      print('e = $e');
    }
    notifyListeners();
  }
  
}
